package com.coin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.CoinRecharge;
import com.coin.dto.UserDto;
import com.coin.feign.SysMemberFeignClient;
import com.coin.mapper.CoinRechargeMapper;
import com.coin.service.CoinRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 李力
 * @description 针对表【coin_recharge(数字货币充值记录)】的数据库操作Service实现
 * @createDate 2025-06-12 10:12:53
 */
@Service
public class CoinRechargeServiceImpl extends ServiceImpl<CoinRechargeMapper, CoinRecharge>
        implements CoinRechargeService {

    @Autowired
    private SysMemberFeignClient sysUserFeignClient;

    /**
     * @Description: 分页查询
     * @Author: 李 力
     * @Date: 2025/6/13
     * @Param:
     * @return: R
     */
    @Override
    public Page<CoinRecharge> findByPage(Page<CoinRecharge> page, Long coinId, Long userId, String userName, String mobile, Byte status, String numMin, String numMax, String startTime, String endTime) {
        LambdaQueryWrapper<CoinRecharge> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(coinId != null, CoinRecharge::getCoinId, coinId)
                .eq(userId != null, CoinRecharge::getUserId, userId)
                .eq(status != null, CoinRecharge::getStatus, status)
                .ge(StrUtil.isNotEmpty(numMin), CoinRecharge::getConfirm, Integer.valueOf(numMin))
                .le(StrUtil.isNotEmpty(numMax), CoinRecharge::getConfirm, Integer.valueOf(numMax))
                .between(StrUtil.isNotEmpty(startTime) && StrUtil.isNotEmpty(endTime), CoinRecharge::getCreated, startTime, endTime);
        Map<Long, UserDto> userDtoMap = new HashMap<>();
        if (userId != null || StrUtil.isNotEmpty(userName) || StrUtil.isNotEmpty(mobile)) {
            userDtoMap = sysUserFeignClient.getBasicUsers2(userId == null ? null : Collections.singletonList(userId), userName, mobile);
            if (CollectionUtil.isEmpty(userDtoMap)) {
                return page;
            }
        }
        Page<CoinRecharge> rechargePage = page(page, queryWrapper);
        List<CoinRecharge> records = rechargePage.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            Map<Long, UserDto> finalUserDtoMap = userDtoMap;
            records.forEach(cashRecharge -> {
                cashRecharge.setUsername(finalUserDtoMap.get(cashRecharge.getUserId()).getUsername());
                cashRecharge.setRealName(finalUserDtoMap.get(cashRecharge.getUserId()).getRealName());
            });
        }
        return rechargePage.setRecords(records);
    }

    /**
     * @Description: 根据用户某种币的Id分页查询
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @Override
    public Page<CoinRecharge> findUserCoinRecharge(Page<CoinRecharge> page, Long coinId, Long userId) {
        LambdaQueryWrapper<CoinRecharge> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(coinId != null, CoinRecharge::getCoinId, coinId)
                .eq(userId != null, CoinRecharge::getUserId, userId);
        return page(page, queryWrapper);
    }
}




